package jjn.round1;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author Jjn
 * @since 2022/8/31 23:25
 */
public class LeetCode205_IsomorphicStrings {

    public boolean isIsomorphic(String s, String t) {
        if (s == null && t == null) {
            return true;
        }
        if (s == null || t == null) {
            return false;
        }
        if (s.length() != t.length()) {
            return false;
        }
        Map<Character, Character> map = new HashMap<>();
        Map<Character, Character> map2 = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char source = s.charAt(i);
            char target = t.charAt(i);
            Character character = map.get(source);
            if (character == null) {
                map.put(source, target);
            } else if (!Objects.equals(character, target)) {
                return false;
            }
            Character character2 = map2.get(target);
            if (character2 == null) {
                map2.put(target, source);
            } else if (!Objects.equals(character2, source)) {
                return false;
            }
        }
        return true;
    }
}
